package com.tjf.lib_core.upload;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.tjf.lib_core.R;


public final class UpdateDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        private final TextView tv_version;
        private final TextView tv_msg;
        private final TextView tv_cancel;
        private final TextView tv_ok;
        private CharSequence mUrl;
        private boolean isMust;

        public Builder(Context context) {
            super(context);

            setContentView(R.layout.update_dialog);
            setAnimStyle(BaseDialog.ANIM_BOTTOM);
            setCancelable(false);

            tv_version = findViewById(R.id.tv_version);
            tv_msg = findViewById(R.id.tv_msg);
            tv_cancel = findViewById(R.id.tv_cancel);
            tv_ok = findViewById(R.id.tv_ok);
            setOnClickListener(tv_cancel, tv_ok);

            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            tv_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    goToDownload();
                }
            });
        }

        /**
         * 设置版本名
         */
        public Builder setVersionName(CharSequence name) {
            tv_version.setText(name);
            return this;
        }

        /**
         * 更新内容
         */
        public Builder setUpLoadContent(String content) {
            String replace = content.replace("\\n", "\n")
                    .replace("<br />","\n");
            String s = replace.toString();
            tv_msg.setText(s);
            return this;
        }

        public Builder setUrl(CharSequence url) {
            mUrl = url;
            return this;
        }
        public Builder isMust(boolean b) {
            isMust = b;
            if (isMust){
                tv_cancel.setVisibility(View.GONE);
            }else {
                tv_cancel.setVisibility(View.VISIBLE);
            }
            return this;
        }



        private void goToDownload() {
            Intent intent = new Intent(getActivity(), DownloadService.class);
            intent.putExtra(UpDateConstants.APK_DOWNLOAD_URL, mUrl);
            getActivity().startService(intent);
        }

    }
}